const { pims } = require('../modules/pjw-pim');
const { moveFiles, deleteFiles } = require('../utils/handleFiles');
const path = require('path');


async function show(req, res, next) {//渲染列表
    const data = req.query;
    let result = await pims.find();
    res.send({
        message: '查询成功',
        code: 200,
        result
    })
}
async function change(req, res, next) {//修改
    const data = req.body;
    console.log(data, 'data');
    if (data.image) {//图片
        const filename = path.basename(data.image);
        moveFiles({
            fromPath: './public/temp',
            toPath: './public/images',
            filename
        })
        deleteFiles('./public/temp');
        data.image = 'http://localhost:3100/./images/' + filename;//更改图片路径数据
    }
    let result = await pims.updateOne({ _id: data._id }, data);
    console.log(result, 'result');
    res.send({
        message: '修改个人信息成功',
        code: 200,
        result
    })
}
async function creat(req, res, next) {//创建
    const data = req.body;
    const result = await pims.create(data);
    if (result) {
        res.send({
            message: '创建成功',
            code: 200
        })
    }
}
module.exports = {
    show, change, creat
}